package com.caine.object;

import com.google.common.io.BaseEncoding;

import java.util.Arrays;

public class ICTestMain {
    public static void main(String[] args) {
//        System.out.println("ic2no:"+ic2no("1547832192"));
//
//        System.out.println("no2ic:"+no2ic("800b425c"));
        String[] strings = parseIdNo(1547832192L);
        System.out.println(Arrays.toString(strings));


    }

    private static String ic2no(String val){
        String hex = Long.toHexString(Long.parseLong(val));
        hex = paddingZero(hex);
        System.out.println(hex);
        String rStr = hex.replaceAll("(\\w{2})(?=.)","$1,");
        String[] strArr = rStr.split(",");
        System.out.println(Arrays.toString(strArr));
        return strArr[3]+strArr[2]+strArr[1]+strArr[0];
    }

    private static String no2ic(String val){
        String rStr = val.replaceAll("(\\w{2})(?=.)","$1,");
        String[] strArr = rStr.split(",");
        System.out.println(Arrays.toString(strArr));
        long l = Long.parseLong(strArr[3] + strArr[2] + strArr[1] + strArr[0], 16);
        return  String.valueOf(l);
    }

    private static String paddingZero(String val){
       return paddingZero(val, 8);
    }

    private static String paddingZero(String val, int len){
        for(int i = len - val.length(); i >0;i--){
           val = "0"+val;
        }
        return val;
    }

    private static String[] parseIdNo(Long card) {
        String physicalNo = null;
        String realNo = null;
        String hexIcNo = null;
        try {
            hexIcNo = Long.toHexString(card);
            physicalNo = hexIcNo.toUpperCase();
            if (hexIcNo.length() %2 != 0) {
                hexIcNo = "0"+hexIcNo;
            }
            //by 20200902根据和李朋成沟通确认物理卡号一定是8位的，不足前面补0
            physicalNo = fillZero(physicalNo,8);

            String reverseHexEndecodeNo = encodeHex(reverse(decodeHex(hexIcNo.toUpperCase())));
            realNo = String.valueOf(Long.parseLong(reverseHexEndecodeNo, 16));

        } catch (Exception e) {
           e.printStackTrace();
        }

        return new String[] {realNo, physicalNo, hexIcNo};
    }

    public static String fillZero(String source,int length){
        StringBuilder sb = new StringBuilder();
        for (int i = 0;i<length - source.length();i++){
            sb.append("0");
        }
        sb.append(source);
        return sb.toString();
    }

    public static byte[] decodeHex(CharSequence input) {
        return BaseEncoding.base16().decode(input);
    }

    public static String encodeHex(byte[] input) {
        return BaseEncoding.base16().encode(input);
    }

    public static byte[] reverse(byte[] var0) {
        if (var0 == null) {
            return null;
        } else {
            int var1 = 0;
            int var2 = var0.length;
            byte[] var3 = new byte[var2];

            while(true) {
                --var2;
                if (var2 < 0) {
                    return var3;
                }

                var3[var2] = var0[var1++];
            }
        }
    }
}
